/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * 初始化构建模板
 * zengbinsi
 * 2020-08-26
 */


import os from 'os';
import path from 'path';
import { exec as spawn} from 'child_process';
import pathUtil from '../libs/utils/path-utils';
import fileUtil from '../libs/utils/file-utils';
import { callSequence } from '../libs/utils/function-call-seq';

const srcPath = pathUtil.pathInTemplateBuild;
const dstPath = pathUtil.pathBuildTemplates;



const initDir = (callback: any)=>{
    console.log('[tool-box]创建目录', dstPath);
    pathUtil.mkPathSync(dstPath);
    callback && callback();
};


export const initBuildTemplateMimiGameWX = (callback: any)=>{
    console.log('[tool-box]正在初始化微信小游戏构建模板...');

    return callSequence([initDir, (cb?: Function) => {
        const sh = `cp -rf ${path.join(srcPath, 'wechatgame')} ${dstPath}`;
        const obj: any = spawn(sh);

        obj.stdout.on('data', (data: any) => {
            console.log('[tool-box]', '初始化微信小游戏构建模板成功!', data);
        });

        obj.stderr.on('data', (err: any) => {
            console.error('[tool-box]', '初始化微信小游戏构建模板失败!', err);
        });

        obj.on('exit', (exitCode: any) => {
            console.log('[tool-box]', '初始化微信小游戏构建模板结束!', 'exit code:', exitCode);
            cb && cb();
        });
    }, callback]);
};